
#include "ChaosProfileManager.hpp"

namespace Chaos
{
	
	/***************************************************************************************************
	**
	** ChsProfileIterator
	**
	***************************************************************************************************/
	ChsProfileIterator::ChsProfileIterator( ChsProfileNode * start )
	{
		m_pCurrentParent = start;
		m_pCurrentChild = m_pCurrentParent->GetChild();
	}


	void	ChsProfileIterator::First(void)
	{
		m_pCurrentChild = m_pCurrentParent->GetChild();
	}


	void	ChsProfileIterator::Next(void)
	{
		m_pCurrentChild = m_pCurrentChild->GetSibling();
	}


	bool	ChsProfileIterator::IsDone(void)const
	{
		return m_pCurrentChild == NULL;
	}


	void	ChsProfileIterator::EnterChild( int index )
	{
		m_pCurrentChild = m_pCurrentParent->GetChild();
		while ( ( m_pCurrentChild != NULL) && (index != 0) ) {
			index--;
			m_pCurrentChild = m_pCurrentChild->GetSibling();
		}

		if ( m_pCurrentChild != NULL ) {
			m_pCurrentParent = m_pCurrentChild;
			m_pCurrentChild = m_pCurrentParent->GetChild();
		}
	}


	void	ChsProfileIterator::EnterParent( void )
	{
		if ( m_pCurrentParent->GetParent() != NULL ) {
			m_pCurrentParent = m_pCurrentParent->GetParent();
		}
		m_pCurrentChild = m_pCurrentParent->GetChild();
	}

	/***************************************************************************************************
	**
	** ChsProfileManager
	**
	***************************************************************************************************/

	ChsProfileManager::ChsProfileManager():
			m_Root(_T("Root"), NULL),
			m_nFrameCounter(0),
			m_n64ResetTime(0)
	{
		 m_pCurrentNode = &m_Root;
	}
	
	ChsProfileManager::~ChsProfileManager()
	{
	}

	void ChsProfileManager::StartProfile(const TCHAR *name)
	{
		if(name != m_pCurrentNode->GetName())
		{
			m_pCurrentNode = m_pCurrentNode->GetSubNode(name);
		}
		m_pCurrentNode->Call();
	}
	void ChsProfileManager::StopProfile()
	{
		if(m_pCurrentNode->Return())
		{
			m_pCurrentNode = m_pCurrentNode->GetParent();
		}
	}

	void ChsProfileManager::Reset( void )
	{ 
		m_Root.Reset(); 
		m_nFrameCounter = 0;
		profileGetTicks(&m_n64ResetTime);
	}

	/***********************************************************************************************
	 * ChsProfileManager::IncrementFrameCounter -- Increment the frame counter                    *
	 *=============================================================================================*/
	void ChsProfileManager::IncrementFrameCounter( void )
	{
		m_nFrameCounter++;
	}


	/***********************************************************************************************
	 * ChsProfileManager::Get_Time_Since_Reset -- returns the elapsed time since last reset         *
	 *=============================================================================================*/
	float ChsProfileManager::GetTimeSinceReset( void )
	{
		__int64 time;
		profileGetTicks(&time);
		time -= m_n64ResetTime;
		return (float)time / profileGetTickRate();
	}


}
